{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #The Number of Full Rounds You Have Played"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfRounds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #你完成的完整对局数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>一款新的在线电子游戏在近期发布，在该电子游戏中，以 <strong>刻钟</strong> 为周期规划若干时长为 <strong>15 分钟</strong> 的游戏对局。这意味着，在 <code>HH:00</code>、<code>HH:15</code>、<code>HH:30</code> 和 <code>HH:45</code> ，将会开始一个新的对局，其中 <code>HH</code> 用一个从 <code>00</code> 到 <code>23</code> 的整数表示。游戏中使用 <strong>24 小时制的时钟</strong> ，所以一天中最早的时间是 <code>00:00</code> ，最晚的时间是 <code>23:59</code> 。</p>\n",
    "\n",
    "<p>给你两个字符串 <code>startTime</code> 和 <code>finishTime</code> ，均符合 <code>\"HH:MM\"</code> 格式，分别表示你 <strong>进入</strong> 和 <strong>退出</strong> 游戏的确切时间，请计算在整个游戏会话期间，你完成的 <strong>完整对局的对局数</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，如果 <code>startTime = \"05:20\"</code> 且 <code>finishTime = \"05:59\"</code> ，这意味着你仅仅完成从 <code>05:30</code> 到 <code>05:45</code> 这一个完整对局。而你没有完成从 <code>05:15</code> 到 <code>05:30</code> 的完整对局，因为你是在对局开始后进入的游戏；同时，你也没有完成从 <code>05:45</code> 到 <code>06:00</code> 的完整对局，因为你是在对局结束前退出的游戏。</li>\n",
    "</ul>\n",
    "\n",
    "<p>如果 <code>finishTime</code> <strong>早于</strong> <code>startTime</code> ，这表示你玩了个通宵（也就是从 <code>startTime</code> 到午夜，再从午夜到 <code>finishTime</code>）。</p>\n",
    "\n",
    "<p>假设你是从 <code>startTime</code> 进入游戏，并在 <code>finishTime</code> 退出游戏，请计算并返回你完成的 <strong>完整对局的对局数</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>startTime = \"12:01\", finishTime = \"12:44\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>你完成了从 12:15 到 12:30 的一个完整对局。\n",
    "你没有完成从 12:00 到 12:15 的完整对局，因为你是在对局开始后的 12:01 进入的游戏。\n",
    "你没有完成从 12:30 到 12:45 的完整对局，因为你是在对局结束前的 12:44 退出的游戏。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>startTime = \"20:00\", finishTime = \"06:00\"\n",
    "<strong>输出：</strong>40\n",
    "<strong>解释：</strong>你完成了从 20:00 到 00:00 的 16 个完整的对局，以及从 00:00 到 06:00 的 24 个完整的对局。\n",
    "16 + 24 = 40\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>startTime = \"00:00\", finishTime = \"23:59\"\n",
    "<strong>输出：</strong>95\n",
    "<strong>解释：</strong>除最后一个小时你只完成了 3 个完整对局外，其余每个小时均完成了 4 场完整对局。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>startTime</code> 和 <code>finishTime</code> 的格式为 <code>HH:MM</code></li>\n",
    "\t<li><code>00 <= HH <= 23</code></li>\n",
    "\t<li><code>00 <= MM <= 59</code></li>\n",
    "\t<li><code>startTime</code> 和 <code>finishTime</code> 不相等</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [the-number-of-full-rounds-you-have-played](https://leetcode.cn/problems/the-number-of-full-rounds-you-have-played/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [the-number-of-full-rounds-you-have-played](https://leetcode.cn/problems/the-number-of-full-rounds-you-have-played/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"09:31\"\\n\"10:14\"', '\"21:30\"\\n\"03:00\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        endmin=int(logoutTime[3:5])//15\n",
    "        if int(loginTime[3:5])%15==0:\n",
    "            startmin=int(loginTime[3:5])//15\n",
    "        else: \n",
    "            startmin=int(loginTime[3:5])//15+1\n",
    "        startnum=int(loginTime[0:2])*4+startmin\n",
    "        endnum=int(logoutTime[0:2])*4+endmin\n",
    "        if int(loginTime[0:2])==int(logoutTime[0:2]) and int(loginTime[3:5])//15==int(logoutTime[3:5])//15 and int(loginTime[3:5])<int(logoutTime[3:5]) :\n",
    "            return 0\n",
    "        if endnum>=startnum:\n",
    "            result=endnum-startnum\n",
    "        else:\n",
    "            result=96-startnum+endnum\n",
    "            \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        #转成分钟计算\n",
    "        t0 = 60 * int(loginTime[:2]) + int(loginTime[3:])\n",
    "        t1 = 60 * int(logoutTime[:2]) + int(logoutTime[3:])\n",
    "\n",
    "        if t1<t0:\n",
    "            t1+=1440\n",
    "        \n",
    "        startpoint=t0//15+1\n",
    "        if t0%15==0:\n",
    "            startpoint-=1\n",
    "        endpoint=t1//15\n",
    "\n",
    "        if endpoint<startpoint:\n",
    "            return 0\n",
    "        return endpoint-startpoint\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        start_h = int(loginTime.split(':')[0])\n",
    "        start_m  = int(loginTime.split(':')[1])\n",
    "        end_h = int(logoutTime.split(':')[0])\n",
    "        end_m = int(logoutTime.split(':')[1])\n",
    "\n",
    "        end = end_h*60 + end_m\n",
    "        if(start_h*60+start_m >= end):\n",
    "            end += 60*24\n",
    "        def getGameMinute(h, m):\n",
    "            if(0 < m <= 15):\n",
    "                m = 15\n",
    "            elif(15 < m <= 30):\n",
    "                m = 30\n",
    "            elif(30 < m <= 45):\n",
    "                m = 45\n",
    "            elif(45 < m):\n",
    "                m = 0\n",
    "                h += 1\n",
    "            return h * 60 + m \n",
    "        start = getGameMinute(start_h, start_m)\n",
    "\n",
    "        return (end - start)//15 if end > start else 0\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, a: str, b: str) -> int:\n",
    "            def cal(a,b):\n",
    "                ah,am = map(int,a.split(':'))\n",
    "                bh,bm = map(int,b.split(':'))\n",
    "                def f(h,x):\n",
    "                    if x == 0:\n",
    "                        return (h,x)\n",
    "                    if x <= 15:\n",
    "                        return (h,15)\n",
    "                    elif x <= 30:\n",
    "                        return (h,30)\n",
    "                    elif x <= 45:\n",
    "                        return (h,45)\n",
    "                    return (h+1,0)\n",
    "                def cnm(h,x):\n",
    "                    return (h,x//15 * 15)\n",
    "                ah,am = f(ah,am)\n",
    "                bh,bm = cnm(bh,bm)\n",
    "                return max((bh*60 + bm - ah * 60 - am)//15 ,0)\n",
    "            return cal(a,b) if a <= b else cal(a,\"24:00\") + cal(\"00:00\",b)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        # 1, get time by minute\n",
    "        # (1) get minute\n",
    "        loginList = list(map(int,loginTime.split(\":\")))\n",
    "        logoutList = list(map(int,logoutTime.split(\":\")))\n",
    "        inH, inM = loginList[0], loginList[1]\n",
    "        outH, outM = logoutList[0], logoutList[1]\n",
    "        \n",
    "        originalINTime = (inH * 60 + inM)\n",
    "        inTime = (inH * 60 + inM)\n",
    "        if inTime % 15 == 0:\n",
    "            inTime = (inTime // 15) * 15\n",
    "        else:\n",
    "            inTime = (inTime // 15 + 1) * 15\n",
    "        \n",
    "        originalOutTime = (outH * 60 + outM)\n",
    "        outTime = ((outH * 60 + outM) // 15) * 15\n",
    "\n",
    "        # (2) return\n",
    "        if originalINTime <= originalOutTime:\n",
    "            # trival case: with in one cycle\n",
    "            if (originalOutTime - originalINTime) // 15 == 0:\n",
    "                return 0\n",
    "            return (outTime - inTime) // 15\n",
    "        else:\n",
    "            return (1440 - inTime + outTime) // 15\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        endmin=int(logoutTime[3:5])//15\n",
    "        if int(loginTime[3:5])%15==0:\n",
    "            startmin=int(loginTime[3:5])//15\n",
    "        else: \n",
    "            startmin=int(loginTime[3:5])//15+1\n",
    "        startnum=int(loginTime[0:2])*4+startmin\n",
    "        endnum=int(logoutTime[0:2])*4+endmin\n",
    "        if int(loginTime[0:2])==int(logoutTime[0:2]) and int(loginTime[3:5])//15==int(logoutTime[3:5])//15 and int(loginTime[3:5])<int(logoutTime[3:5]) :\n",
    "            return 0\n",
    "        if endnum>=startnum:\n",
    "            result=endnum-startnum\n",
    "        else:\n",
    "            result=96-startnum+endnum\n",
    "            \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        # 转化为分钟\n",
    "        t0 = 60 * int(startTime[:2]) + int(startTime[3:])\n",
    "        t1 = 60 * int(finishTime[:2]) + int(finishTime[3:])\n",
    "        if t1 < t0:\n",
    "            # 此时 finishTime 为第二天\n",
    "            t1 += 1440\n",
    "        # 第一个小于等于 finishTime 的完整对局的结束时间\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0)) // 15\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        h, m = int(loginTime[:2]), int(loginTime[3:])\n",
    "        eh, hm = int(logoutTime[:2]), int(logoutTime[3:])\n",
    "        def cal(x, y):\n",
    "            return x * 60 + y\n",
    "        if eh * 60 + hm < h * 60 + m:\n",
    "            eh += 24\n",
    "        while m % 15 != 0:\n",
    "            m += 1\n",
    "            if m == 0:\n",
    "                h += 1\n",
    "        if cal(eh, hm) < cal(h, m):\n",
    "            return 0\n",
    "        z = cal(eh, hm) - cal(h, m)\n",
    "        return z // 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        def go(s):\n",
    "            h = int(s[:2])\n",
    "            m = int(s[-2:])\n",
    "            return h, m\n",
    "        \n",
    "        h1, m1 = go(loginTime)\n",
    "        h2, m2 = go(logoutTime)\n",
    "        m1 = (m1 + 14) // 15\n",
    "        m2 //= 15\n",
    "        s1 = h1 * 4 + m1\n",
    "        s2 = h2 * 4 + m2\n",
    "        if loginTime > logoutTime:\n",
    "            s2 += 96\n",
    "        return max(s2 - s1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        endmin=int(logoutTime[3:5])//15\n",
    "        if int(loginTime[3:5])%15==0:\n",
    "            startmin=int(loginTime[3:5])//15\n",
    "        else: \n",
    "            startmin=int(loginTime[3:5])//15+1\n",
    "        startnum=int(loginTime[0:2])*4+startmin\n",
    "        endnum=int(logoutTime[0:2])*4+endmin\n",
    "        if int(loginTime[0:2])==int(logoutTime[0:2]) and int(loginTime[3:5])//15==int(logoutTime[3:5])//15 and int(loginTime[3:5])<int(logoutTime[3:5]) :\n",
    "            return 0\n",
    "        if endnum>=startnum:\n",
    "            result=endnum-startnum\n",
    "        else:\n",
    "            result=96-startnum+endnum\n",
    "            \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        # python中 print(int(\"01\")) 会显示 1 不会报错\n",
    "\n",
    "        # def getInt(time_str):\n",
    "        #     if time_str.startswith(\"0\"):\n",
    "        #         return int(time_str[1])\n",
    "        #     return int(time_str)\n",
    "\n",
    "        # 统一转成分钟计算\n",
    "        def toMin(timeInfo):\n",
    "            h,m = timeInfo.split(\":\")\n",
    "            return int(h)*60+int(m)\n",
    "\n",
    "        # 虽然题目写的是 一小时内的 时间节点 是 15,30,45 这样\n",
    "        # 但其实只要 考虑 时间是不是 超过 15分钟，30分钟，45分钟，即可\n",
    "        # 不需要关心 具体 时间点，因为，超过 15分钟的时间，一定会经过这些时间点\n",
    "        # 反之，一定不会\n",
    "\n",
    "\n",
    "\n",
    "        startMin = toMin(loginTime)\n",
    "        endMin = toMin(logoutTime)\n",
    "\n",
    "        if endMin < startMin:\n",
    "            endMin += 24*60\n",
    "\n",
    "        import math\n",
    "\n",
    "        endMin = endMin // 15 * 15\n",
    "\n",
    "        ans = (endMin-startMin) //15\n",
    "\n",
    "        return max(0,ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 模拟\n",
    "    # 转化为分钟\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        # 转化为分钟\n",
    "        t0 = 60 * int(startTime[:2]) + int(startTime[3:])\n",
    "        t1 = 60 * int(finishTime[:2]) + int(finishTime[3:])\n",
    "        if t1 < t0:\n",
    "            # 此时 finishTime 为第二天\n",
    "            t1 += 1440\n",
    "\n",
    "        # 第一个小于等于 finishTime 的完整对局的结束时间\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0)) // 15\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        start_h = int(loginTime.split(':')[0])\n",
    "        start_m  = int(loginTime.split(':')[1])\n",
    "        end_h = int(logoutTime.split(':')[0])\n",
    "        end_m = int(logoutTime.split(':')[1])\n",
    "\n",
    "        end = end_h*60 + end_m\n",
    "        if(start_h*60+start_m >= end):\n",
    "            end += 60*24\n",
    "        def getGameMinute(h, m):\n",
    "            if(0 < m <= 15):\n",
    "                m = 15\n",
    "            elif(15 < m <= 30):\n",
    "                m = 30\n",
    "            elif(30 < m <= 45):\n",
    "                m = 45\n",
    "            elif(45 < m):\n",
    "                m = 0\n",
    "                h += 1\n",
    "            return h * 60 + m \n",
    "        start = getGameMinute(start_h, start_m)\n",
    "\n",
    "        return (end - start)//15 if end > start else 0\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        def duration(start: str, end: str) -> int:\n",
    "            a,b = int(start[0:2]),int(start[3:])\n",
    "            c,d = int(end[0:2]),int(end[3:])\n",
    "            time1,time2 = 60*a+b,60*c+d\n",
    "            if time2 < time1:\n",
    "                return 24*60+time2-time1\n",
    "            return time2-time1\n",
    "        \n",
    "        tmp1 = 15 - int(loginTime[3:])%15 if int(loginTime[3:])%15 else 0\n",
    "        tmp2 = int(logoutTime[3:])%15\n",
    "        return (duration(loginTime,logoutTime)-tmp1-tmp2)//15 if (duration(loginTime,logoutTime)-tmp1-tmp2)//15 > 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, a: str, b: str) -> int:\n",
    "            def cal(a,b):\n",
    "                ah,am = map(int,a.split(':'))\n",
    "                bh,bm = map(int,b.split(':'))\n",
    "                def f(h,x):\n",
    "                    if x == 0:\n",
    "                        return (h,x)\n",
    "                    if x <= 15:\n",
    "                        return (h,15)\n",
    "                    elif x <= 30:\n",
    "                        return (h,30)\n",
    "                    elif x <= 45:\n",
    "                        return (h,45)\n",
    "                    return (h+1,0)\n",
    "                def cnm(h,x):\n",
    "                    return (h,x//15 * 15)\n",
    "                ah,am = f(ah,am)\n",
    "                bh,bm = cnm(bh,bm)\n",
    "                print(ah,am,bh,bm)\n",
    "                return max((bh*60 + bm - ah * 60 - am)//15 ,0)\n",
    "            # print(cal(a,\"24:00\"))\n",
    "            return cal(a,b) if a <= b else cal(a,\"24:00\") + cal(\"00:00\",b)\n",
    "\n",
    "#         \"12:01\"\n",
    "# \"12:44\"\n",
    "# \"20:00\"\n",
    "# \"06:00\"\n",
    "# \"00:00\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\r\n",
    "        a,b = map(int,loginTime.split(':'))\r\n",
    "        c,d = map(int,logoutTime.split(':'))\r\n",
    "        t0 = a*60+b\r\n",
    "        t1 = c*60+d\r\n",
    "        if t0 > t1:\r\n",
    "            t1 += 24*60\r\n",
    "        t0 = (t0+14)//15\r\n",
    "        t1 = t1//15\r\n",
    "        return max(0,t1-t0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        def f(t1, t2):\n",
    "            a1 = (int(t1[0]) * 10 + int(t1[1])) * 60 + int(t1[-2]) * 10 + int(t1[-1])\n",
    "            a2 = (int(t2[0]) * 10 + int(t2[1])) * 60 + int(t2[-2]) * 10 + int(t2[-1])\n",
    "            if a1 == a2:\n",
    "                return 0\n",
    "            if a2 == 0:\n",
    "                a2 = 1440\n",
    "            b1 = (a1 + 14) // 15 * 15 \n",
    "            b2 = a2 // 15 * 15\n",
    "            return (b2 - b1) // 15 if b2 > b1 else 0\n",
    "        if loginTime < logoutTime:\n",
    "            return f(loginTime, logoutTime)\n",
    "        else:\n",
    "            return f(loginTime, '00:00') + f('00:00', logoutTime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        start_time_list = loginTime.split(\":\")\n",
    "        start_m = int(start_time_list[0]) * 60 + int(start_time_list[1])\n",
    "\n",
    "        end_time_list = logoutTime.split(\":\")\n",
    "        end_m = int(end_time_list[0]) * 60 + int(end_time_list[1])\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        if end_m < start_m:\n",
    "            end_m += 24 * 60\n",
    "\n",
    "        start_m = start_m // 15 * 15 + 15 if start_m % 15 else start_m\n",
    "        ans = 0 if start_m >= end_m else (end_m - start_m) // 15\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        starth=int(loginTime[:2])\n",
    "        startm=int(loginTime[3:])\n",
    "        endh=int(logoutTime[:2])\n",
    "        endm=int(logoutTime[3:])\n",
    "        sign=0\n",
    "        if loginTime>logoutTime:\n",
    "            sign=1\n",
    "        start=datetime.datetime(2001,1,1,starth,startm)\n",
    "        if sign==1:\n",
    "            end=datetime.datetime(2001,1,2,endh,endm)\n",
    "        else:\n",
    "            end=datetime.datetime(2001,1,1,endh,endm)\n",
    "        temp=end-start\n",
    "        if temp.seconds//900<=0:\n",
    "            return 0\n",
    "        if startm<15 and startm>0:\n",
    "            startm=15\n",
    "        elif startm>15 and startm<30:\n",
    "            startm=30\n",
    "        elif startm<45 and startm>30:\n",
    "            startm=45\n",
    "        elif startm>45 and startm<=59:\n",
    "            starth+=1\n",
    "            if starth>=24:\n",
    "                starth=0\n",
    "            startm=0\n",
    "        start=datetime.datetime(2001,1,1,starth,startm)\n",
    "        if sign==1:\n",
    "            end=datetime.datetime(2001,1,2,endh,endm)\n",
    "        else:\n",
    "            end=datetime.datetime(2001,1,1,endh,endm)\n",
    "        temp=end-start\n",
    "        m=temp.seconds\n",
    "        return m//900\n",
    "\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        def toNearestRound(time: str, up: bool) -> int:\n",
    "            h, m = int(time[:2]), int(time[3:])\n",
    "            numOfMinutes = h * 60 + m\n",
    "            return ceil(numOfMinutes / 15) if up else numOfMinutes // 15\n",
    "        \n",
    "        loginRound = toNearestRound(loginTime, True)\n",
    "        logoutRound = toNearestRound(logoutTime, False)\n",
    "        if logoutTime >= loginTime:\n",
    "            return max(logoutRound - loginRound, 0)\n",
    "        return ((logoutRound - loginRound) + 4 * 24) % (4 * 24)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "\n",
    "        def num(start_hour,start_minute,end_hour,end_minute):\n",
    "            count=0\n",
    "            if start_hour==end_hour:\n",
    "                for i in [0,15,30,45]:\n",
    "                    if start_minute<=i<i+15<=end_minute:\n",
    "                        count+=1\n",
    "                return count\n",
    "            else:\n",
    "                count+=3*(end_hour-start_hour-1)\n",
    "                count+=end_hour-start_hour-1\n",
    "                if start_minute==0:\n",
    "                    count+=4\n",
    "                elif 0<start_minute<=15:\n",
    "                    count+=3\n",
    "                elif 15<start_minute<=30:\n",
    "                    count+=2\n",
    "                elif 30<start_minute<=45:\n",
    "                    count+=1\n",
    "                if 15<=end_minute<30:\n",
    "                    count+=1\n",
    "                elif 30<=end_minute<45:\n",
    "                    count+=2\n",
    "                elif 45<=end_minute<=59:\n",
    "                    count+=3\n",
    "                return count\n",
    "\n",
    "        start_hour = int(loginTime[0:2])\n",
    "        start_minute = int(loginTime[3:])\n",
    "        end_hour = int(logoutTime[0:2])\n",
    "        end_minute = int(logoutTime[3:])\n",
    "\n",
    "        flag=False\n",
    "\n",
    "        if start_hour>end_hour:\n",
    "            flag=True\n",
    "        elif start_hour==end_hour and start_minute>end_minute:\n",
    "            flag=True\n",
    "\n",
    "        if flag:\n",
    "            count=1\n",
    "            if start_hour==23 and start_minute>45:\n",
    "                count=0\n",
    "\n",
    "            return num(start_hour, start_minute, 23,59)+num(0,0,end_hour,end_minute)+count\n",
    "        else:\n",
    "            return num(start_hour,start_minute,end_hour,end_minute)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        # two edge cases:\n",
    "        # 1. logout < login, start from login to midnight, & midnight to logout\n",
    "        # 2. 15 mins is a round, (x means after :)if 15 < x < 30 (16-29), shift to next round\n",
    "                               # if 0 < x < 15 (1-14)\n",
    "        # login = loginTime.split(':')\n",
    "        # logout = logoutTime.split(':')\n",
    "        # login_int, login_fra = int(login[0]), int(login[1])\n",
    "        # logout_int, logout_fra = int(logout[0]), int(logout[1])\n",
    "        # res = 0\n",
    "        # res_int = 0\n",
    "        # res_fra = 0\n",
    "        # # print(login)\n",
    "        # # 1\n",
    "        # # int part\n",
    "        # if logout_int < login_int:\n",
    "        #     res_int = 12 - login_int + logout_int\n",
    "        # else:\n",
    "        #     res_int = logout_int - login_int\n",
    "        # res += res_int\n",
    "        # # fra part\n",
    "        # # after : , is not int, may 1 - 14, 16 - 29, 31 - 59\n",
    "        # if login_fra < logout_fra:\n",
    "        #   if login_fra % 15:\n",
    "        #     if 0 < login_fra < 15:\n",
    "\n",
    "        # 转化为分钟\n",
    "        t0 = int(loginTime[:2])*60 + int(loginTime[3:])\n",
    "        t1 = int(logoutTime[:2])*60 + int(logoutTime[3:])\n",
    "        if t1 < t0:\n",
    "          t1 += 24*60\n",
    "        t1 = t1 // 15 * 15 # 先//是为了取整，再*是为了还原，每次都会得到最接近的int，往小取\n",
    "        return max(0, (t1-t0)//15)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "           \n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        start_time_list = loginTime.split(\":\")\n",
    "        start_m = int(start_time_list[0]) * 60 + int(start_time_list[1])\n",
    "\n",
    "        end_time_list = logoutTime.split(\":\")\n",
    "        end_m = int(end_time_list[0]) * 60 + int(end_time_list[1])\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        if end_m < start_m:\n",
    "            end_m += 24 * 60\n",
    "\n",
    "        start_m = start_m // 15 * 15 + 15 if start_m % 15 else start_m\n",
    "        ans = 0 if start_m >= end_m else (end_m - start_m) // 15\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        t0 = 60 * int(loginTime[:2]) + int(loginTime[3:])\n",
    "        t1 = 60 * int(logoutTime[:2]) + int(logoutTime[3:])\n",
    "        if t1 < t0:\n",
    "            t1 += 1440\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0)) // 15\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, start: str, end: str) -> int:\n",
    "        t1 = int(start[0:2]) * 60 + int(start[3:5])\n",
    "        t2 = int(end[0:2]) * 60 + int(end[3:5])\n",
    "        if t2 < t1:\n",
    "            t2 += 24 * 60 \n",
    "        \n",
    "        t1 = ceil(t1 / 15)\n",
    "        t2 = floor(t2 / 15)\n",
    "        return max(0, t2 - t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        # 转化为分钟\n",
    "        t0 = 60 * int(startTime[:2]) + int(startTime[3:])\n",
    "        t1 = 60 * int(finishTime[:2]) + int(finishTime[3:])\n",
    "        if t1 < t0:\n",
    "            # 此时 finishTime 为第二天\n",
    "            t1 += 1440\n",
    "        # 第一个小于等于 finishTime 的完整对局的结束时间\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0)) // 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# encoding=utf-8\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        la = loginTime.split(\":\")\n",
    "        lta = logoutTime.split(\":\")\n",
    "        s_h, s_m, e_h, e_m = int(la[0]), int(la[1]), int(lta[0]), int(lta[1])\n",
    "        # 转为\n",
    "        if e_h < s_h or (e_h == s_h and e_m < s_m):\n",
    "            e_h += 24\n",
    "\n",
    "        # 按时间来计算的游戏数\n",
    "        r = (e_h-s_h)*4\n",
    "        # 开始分钟数影响偏差\n",
    "        if s_m != 0:\n",
    "            r -= s_m//15\n",
    "            if s_m%15 != 0:\n",
    "                r -= 1\n",
    "        if e_m != 0:\n",
    "            r += e_m//15\n",
    "        return 0 if r < 0 else r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        h1, m1 = map(int, startTime.split(':'))\n",
    "        h2, m2 = map(int, finishTime.split(':'))\n",
    "        \n",
    "        a1 = h1 * 60 + m1\n",
    "        a2 = h2 * 60 + m2\n",
    "        \n",
    "        if a2 < a1:\n",
    "            a2 += 24 * 60\n",
    "        \n",
    "        a = ceil(a1 / 15)\n",
    "        b = floor(a2 / 15)\n",
    "        return max(0, b - a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        re_start = re.match('([0-9]+):([0-9]+)', startTime)\n",
    "        start = int(re_start.group(1)) * 60 + int(re_start.group(2))\n",
    "        re_finish = re.match('([0-9]+):([0-9]+)', finishTime)\n",
    "        finish = int(re_finish.group(1)) * 60 + int(re_finish.group(2))\n",
    "        if start > finish:\n",
    "            finish += 60 * 24\n",
    "        return max(math.floor(finish / 15) - math.ceil(start / 15), 0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        inm = int(loginTime[0:2]) * 60 + int(loginTime[3:])\n",
    "        otm = int(logoutTime[0:2]) * 60 + int(logoutTime[3:])\n",
    "        if otm < inm:\n",
    "            otm += 24*60\n",
    "        a = ceil(inm/15)\n",
    "        b = floor(otm/15)\n",
    "        return max(0, b - a)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        startfirst=int(startTime[0:2])\n",
    "        finishfirst=int(finishTime[0:2])\n",
    "        \n",
    "        startsecond=int(startTime[3:5])\n",
    "        finishsecond=int(finishTime[3:5])\n",
    "\n",
    "        if finishfirst<startfirst or (finishfirst==startfirst and finishsecond<startsecond):\n",
    "            finishfirst+=24\n",
    "        \n",
    "        #return(startfirst,finishfirst,startsecond,finishsecond)\n",
    "        if finishfirst-startfirst>1:\n",
    "            aa=(finishfirst-startfirst-1)*4\n",
    "        else:\n",
    "            aa=0\n",
    "        \n",
    "        #return finishfirst-startfirst\n",
    "        finishfirst-=aa//4\n",
    "        #return startfirst,finishfirst\n",
    "\n",
    "        \n",
    "        bb=0\n",
    "        if startfirst==finishfirst:\n",
    "            if finishsecond>=45 and startsecond<=30:\n",
    "                bb+=1\n",
    "            if finishsecond>=30 and startsecond<=15:\n",
    "                bb+=1\n",
    "            if finishsecond>=15 and startsecond==0:\n",
    "                #return aa\n",
    "                bb+=1\n",
    "        \n",
    "        elif startfirst+1==finishfirst:\n",
    "            \n",
    "            if startsecond==0:\n",
    "                bb+=1\n",
    "            if startsecond<=15:\n",
    "                bb+=1\n",
    "            if startsecond<=30:\n",
    "                bb+=1\n",
    "            if startsecond<=45:\n",
    "                bb+=1\n",
    "            if finishsecond>=15:\n",
    "                bb+=1\n",
    "            if finishsecond>=30:\n",
    "                bb+=1\n",
    "            if finishsecond>=45:\n",
    "                bb+=1\n",
    "        \n",
    "        return aa+bb\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        t0=60*int(loginTime[:2])+int(loginTime[3:])\n",
    "        t1=60*int(logoutTime[:2])+int(logoutTime[3:])\n",
    "\n",
    "        if t1<t0:\n",
    "            t1+=1440\n",
    "        if t0%15:\n",
    "            t0=(t0//15+1)*15\n",
    "        t1=t1//15*15\n",
    "        res=(t1-t0)//15\n",
    "        return max(0,res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 模拟\n",
    "    # 转化为分钟\n",
    "    def numberOfRounds(self, startTime: str, finishTime: str) -> int:\n",
    "        # 转化为分钟\n",
    "        t0 = 60 * int(startTime[:2]) + int(startTime[3:])\n",
    "        t1 = 60 * int(finishTime[:2]) + int(finishTime[3:])\n",
    "        if t1 < t0:\n",
    "            # 此时 finishTime 为第二天\n",
    "            t1 += 1440\n",
    "\n",
    "        # 第一个小于等于 finishTime 的完整对局的结束时间\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0)) // 15\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        start_hh = int(loginTime.split(\":\")[0])\n",
    "        start_mm = int(loginTime.split(\":\")[1])\n",
    "        stop_hh = int(logoutTime.split(\":\")[0])\n",
    "        stop_mm = int(logoutTime.split(\":\")[1])\n",
    "        \n",
    "        game_time = 0\n",
    "        if stop_hh < start_hh or stop_hh == start_hh and stop_mm < start_mm:\n",
    "            stop_hh = 24 + stop_hh\n",
    "        if stop_hh - start_hh - 1 > 0:\n",
    "            game_time += (stop_hh - start_hh - 1) * 4\n",
    "        if stop_hh == start_hh:\n",
    "            if start_mm <= 0 and stop_mm >= 15:\n",
    "                game_time += 1\n",
    "            if start_mm <= 15 and stop_mm >= 30:\n",
    "                game_time += 1\n",
    "            if start_mm <= 30 and stop_mm >= 45:\n",
    "                game_time += 1\n",
    "        else:\n",
    "            game_time += (60 - start_mm) // 15\n",
    "            game_time += stop_mm // 15\n",
    "        \n",
    "        return game_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        \"\"\"\n",
    "        判断两个时间是否在同一天\n",
    "        1.如果在同一天:满打满算24小时最多玩24*4=96局,\n",
    "        a = login之前的时间占用了多少局时间, b = 从00:00到logout能玩多少局, 答案是 b-a\n",
    "        2.如果不在同一天:分别计算login之后当天玩的局数 + logout之前当天玩的局数\n",
    "        \"\"\"\n",
    "        # 分别提取login,logout的小时与分钟数\n",
    "        loginHour, logoutHour = int(loginTime[:2]), int(logoutTime[:2])\n",
    "        loginMin, logoutMin = int(loginTime[3:]), int(logoutTime[3:])\n",
    "        # 判断是否同一天\n",
    "        overNight = False\n",
    "        if logoutHour < loginHour or (logoutHour == loginHour and logoutMin < loginMin):\n",
    "            overNight = True\n",
    "        # 情况1,进入与退出在同一天\n",
    "        if overNight is False:\n",
    "            a = loginHour * 4 + math.ceil(loginMin/15)\n",
    "            b = logoutHour * 4 + logoutMin//15\n",
    "            return max(0, b-a)\n",
    "        # 情况2,进入与退出不在同一天\n",
    "        # login之后当天能玩的局数 = 小时数到24点满打满算能玩的局数-分钟数占用的局数\n",
    "        a = (24-loginHour)*4 - math.ceil(loginMin/15)\n",
    "        b = logoutHour*4 + logoutMin//15  # logout之前当天玩的局数=小时数*4 + 分钟数能玩的局数\n",
    "        return a + b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        # 完整对局=》将小时的数值换算成对局数\n",
    "        # 23*4 = 92\n",
    "        # 判断是否已经间隔了一天\n",
    "        start_hour = loginTime.split(':')[0]\n",
    "        start_mini = loginTime.split(':')[1]\n",
    "        end_hour = logoutTime.split(':')[0]\n",
    "        end_mini = logoutTime.split(':')[1]\n",
    "        # 未通宵的情况下：\n",
    "        start_ = int(start_hour) * 60 + int(start_mini)\n",
    "        end_ = int(end_hour) * 60 + int(end_mini)\n",
    "        if start_ > end_:\n",
    "            end_ += 1440\n",
    "        end_ = end_ // 15 * 15\n",
    "        return max(0,(end_-start_)//15)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:\n",
    "        t0 = int(loginTime[:2]) * 60 + int(loginTime[3:])\n",
    "        t1 = int(logoutTime[:2]) * 60 + int(logoutTime[3:])\n",
    "        if t1 < t0:\n",
    "            t1 += 1440\n",
    "        t1 = t1 // 15 * 15\n",
    "        return max(0, (t1 - t0) // 15)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
